<?
include_once("../codelibrary/inc/variables.php");//Using variables.php for Database Connectivity
include_once("../codelibrary/inc/functions.php");//Using functions.php for using getAnyTableWhereData()


//if Welder exists  --redirecting to previous page					
if(!$terms)
{
	
	$hflds="";
			if (!empty($HTTP_POST_VARS)) 
			{
				reset($HTTP_POST_VARS);
				while (list($k,$v)=each($HTTP_POST_VARS)) 
				{
					   ${$k}=$v;
						$hflds .= "<input type=hidden name='".$k."' value='".$v."'>";
				}	
			}	
				$hflds .= "<input type=hidden name=err value='termsnotaccepted'>";
				echo "<form name=f1 action='accountenrollment.php' method=post>";
				echo $hflds."</form>";
				echo "<script>document.f1.submit();</script>";
				die();
}

$Record=getAnyTableWhereData($administrator_info_table," and UserName='$TRLEN_UserName' ");

//if User exists  --redirecting to previous page					
if($Record)
{
	
	$hflds="";
			if (!empty($HTTP_POST_VARS)) 
			{
				reset($HTTP_POST_VARS);
				while (list($k,$v)=each($HTTP_POST_VARS)) 
				{
					   ${$k}=$v;
						$hflds .= "<input type=hidden name='".$k."' value='".$v."'>";
				}	
			}	
				$hflds .= "<input type=hidden name=err value='alreadyexists'>";
				echo "<form name=f1 action='accountenrollment.php' method=post>";
				echo $hflds."</form>";
				echo "<script>document.f1.submit();</script>";
				die();
}
//if User does not exist --Adding it as new User
else
{	
    ###############   Inserting Information Of Business Into business_info_table #############
    //$BusinessCategory - Work To Do:- Business Category (Code Written In Paper)
	$BusinessCategory = traverse($array_business_category,$TR_BusinessCategory,1,'','');
	$CompanyName=$TR_Company;
	$Street=$TR_Street;
	$City=$TRC_City;
	$State=$TR_State; //Work To Do:- Converting Short Form Of States Into There Full forms e.g: AK - Alaska
	$Country=$TR_Country;
	$ZipCode=$TR_Zip;
	$TimeZone=$TR_TimeZone;
	$TrialMemberShip=$TR_TrialMemberShip;
	
	insertData($business_info_table);
	
	$BusinessID = mysql_insert_id();
	###############   Inserting Information Of Administrator Of Business Into administrator_info_table #############
	$FirstName=$TRFN_FirstName;
	$LastName=$TRFN_LastName;
	$Phone=$PHR_Phone;
	$Extension=$TR_Extension;
	$Email=$TREF_Email;
	$Status=$GL_active;
	$LoginIP=$REMOTE_ADDR;
	$UserName=$TRLEN_UserName;
	$Password=$TR_Password;
	$UserID='';
	if($IN_Access_Days!="")
	{
	   $AccessDays=$IN_Access_Days;
	}
	else
	{
       $AccessDays=0;
	}
	$DateAdded=date("Y-m-d H:i:s");
	insertData($administrator_info_table);
	$AdministratorID=mysql_insert_id();
	
	//For Sending Confirmation Mail To The Administrator Of the Account
	$subject="Enrollment confirmation";
	
	$login_url=$site_url."/business/login.php";

	$guide_download_url=$site_url."/images/AppointmentQuest.pdf";
	
	$provider_url=$site_url."/provider/provider.php?aid=$AdministratorID";
	
	$message="Dear $FirstName $LastName,\n\n";
	$message.= "Thank you for your interest in AppointmentQuest scheduling services!\n";
	$message.= "Your Online Appointment Manager account #$AdministratorID has been created.\n\n";
	$message.= "You can access your account at any time at:\n\n";
	
	$message.= "$login_url\n\n";
	$message.= "We strongly recommend you to download and print Online Appointment
	Manager Account Setup Guide. It contains very detailed information on
	how to setup your account properly, as well as answers to most
	frequently answered questions related to online scheduling
	configuration. You can download the guide from:\n\n";
	$message.= "$guide_download_url\n\n";
	
	$message.= "Once you complete your new account setup to define business hours,
	create schedules and personnel records, your online scheduling pages
	will be automatically published at the URL listed below:\n\n";
	
	$message.= "$provider_url\n\n";
	$message.= "For detailed instructions on how to integrate online appointment
	scheduling with your web site and where to direct your customers to
	make appointments, login to your Online Appointment Manager account
	and go to:\n\n";
	$message.= "Account - Setup Center - Web Site Integration\n\n";
	$message.= "ACCOUNT\n";
	$message.= " Business Name: $CompanyName\n";
	$message.= " Account Number: $AdministratorID\n";
	$message.= " Business Category: $BusinessCategory\n";
	$message.= " Address: \n";
	$message.= " $Street\n";
	$message.= "  $City,$State $ZipCode\n";
	$message.= "  $Country\n";
	$message.= "Phone: $Phone\n";
	$message.= "Fax: \n";
	$message.= "E-mail: $Email\n";
	$message.= "Web Site: \n";
	$message.= "Time Zone: $TimeZone\n\n";
	$message.= "MEMBERSHIP\n";
	$message.= " Membership: $TrialMemberShip\n\n";
	$message.= "ACCOUNT ADMINISTRATOR\n";
	$message.= "First Name: $FirstName\n";
	$message.= "Last Name: $LastName\n";
	$message.= "Phone: $Phone ext. $Extension\n";
	$message.= "E-mail: $Email\n";
	$message.= "Username: $UserName\n";
	$message.= "Password: xxxxxxxx (masked for security reasons)\n\n";
	$message.= "Please, do not reply to this message.\n\n";
	
	// In case any of our lines are larger than 70 characters, we should use wordwrap()
	$message = wordwrap($message, 70);
	
	// To send HTML mail, the Content-type header must be set
	/*$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";*/
	
	// Send
	mail($Email,$subject, $message);
	
	header("location: account-enrollment-details.php?bid=$BusinessID&aid=$AdministratorID&msg=Added");
	die();
}